﻿using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Data;
using System.Text;

namespace ZHDJ.Data.Models.Center
{
    /// <summary>
    /// 流动党员找党组织
    /// </summary>
    public class PartyMembersFindOrg : EntityBaseOperation
    {
        #region 字段属性
        /// <summary>
        /// 用户名
        /// </summary>
        
        public string UserName { get; set; }

        /// <summary>
        /// 性别
        /// </summary>
        
        public string Gender { get; set; }

        /// <summary>
        /// 生日
        /// </summary>
        
        public string Birthday { get; set; }

        /// <summary>
        /// 组织ID
        /// </summary>
        public string BranchID { get; set; }

        /// <summary>
        /// 身份证
        /// </summary>
        public string IdentityCard { get; set; }

        /// <summary>
        /// 手机号码
        /// </summary>
        public string MobileNo { get; set; }

        /// <summary>
        /// qq
        /// </summary>
        public string QQNumber { get; set; }

        /// <summary>
        /// 籍贯
        /// </summary>
        public string NativePlace { get; set; }

        /// <summary>
        /// 民族
        /// </summary>
        public string Nation { get; set; }

        /// <summary>
        /// 学历
        /// </summary>
        public string Educational { get; set; }

        /// <summary>
        /// 职务
        /// </summary>
        public string Post { get; set; }

        /// <summary>
        /// 党员编号
        /// </summary>
        public string PartyCard { get; set; }

        /// <summary>
        /// 入党时间
        /// </summary>
        public DateTime? JoinPartyDate { get; set; }

        /// <summary>
        /// 党组织工作所在地
        /// </summary>
        public string PartyJobAddress { get; set; }

        /// <summary>
        /// 居住地址
        /// </summary>
        public string Address { get; set; }
        
        /// <summary>
        /// Remark
        /// </summary>
        public string Remark { get; set; }

        /// <summary>
        /// 审核党工委组织ID
        /// </summary>
        public string PartyWorkingCommittee { get; set; }

        /// <summary>
        /// 状态  1、提交申请  2、审核通过，3、驳回
        /// </summary>
        public string Status { get; set; }
        /// <summary>
        /// 审核意见
        /// </summary>
        public string AduitContent { get; set; }
        /// <summary>
        /// 原党支部
        /// </summary>
        public string OriginalBranch { get; set; }
        #endregion

        #region 查询管理
        public static DataSet GetGridData(int pageIndex, int pageSize, string userName, string branchId,string loginId="")
        {
            List<MySqlParameter> parameters = new List<MySqlParameter>()
            {
                new MySqlParameter()
                {
                    DbType = DbType.Int32,
                    ParameterName = "Begin",
                    Value = (pageIndex)*pageSize
                },
                new MySqlParameter()
                {
                    DbType = DbType.Int32,
                    ParameterName = "PageSize",
                    Value = pageSize
                }
            };


            StringBuilder sb = new StringBuilder();
            sb.Append(@"select SQL_CALC_FOUND_ROWS  a.ID,UserName,Birthday,MobileNo,a.CreateTime,BranchName,a.Status,
case Status when  1 then '待审核' when  2 then '审核通过' when  3 then '驳回'  else   '待审核' end StatusName,
if(Gender='0','男','女') as GenderName from tblpartymembersfindorg a
left join tblpartyorgbranch b on a.BranchID=b.BranchID where 1=1");

            if (!string.IsNullOrEmpty(userName))
            {
                sb.Append(" and UserName like ?UserName ");
                parameters.Add(
                    new MySqlParameter()
                    {
                        DbType = DbType.String,
                        ParameterName = "UserName",
                        Value = string.Format("%{0}%", userName)
                    });
            }
            if (!string.IsNullOrEmpty(branchId))
            {
                sb.Append(" and a.BranchID in (select ChildBranchID from tblpartyorgbranchrelation brr where brr.BranchID=?BranchID)");
                parameters.Add(
                    new MySqlParameter()
                    {
                        DbType = DbType.String,
                        ParameterName = "BranchID",
                        Value = string.Format("{0}", branchId)
                    });
            }
            if (!string.IsNullOrEmpty(loginId))
            {
                sb.Append(" and a.CreateBy =?CreateBy ");
                parameters.Add(
                    new MySqlParameter()
                    {
                        DbType = DbType.String,
                        ParameterName = "CreateBy",
                        Value = loginId
                    });
            }

            sb.Append(" order by Status desc, a.CreateTime desc LIMIT ?Begin ,?PageSize;SELECT FOUND_ROWS();");
            return MySqlHelper.ExecuteDataSet(sb.ToString(), parameters.ToArray());
        }
        #endregion
    }
}
